<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h2>get 请求传参</h2>
    <button>聊天机器人</button>
    <script>
        const api_robot = `http://ajax-api.itheima.net/api/robot?spoken=你吃饭了吗`
        const btn = document.querySelector('button')
        btn.onclick = () => {
            getData(api_robot)
        }

        const getData = (url) => {
            const xhr = new XMLHttpRequest()
            xhr.open('GET', url)
            xhr.send()
            xhr.responseType = 'json'
            xhr.onreadystatechange = () => {
                if (xhr.readyState !== 4) return
                if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                    console.log(xhr.response)
                } else {
                    console.log('失败')
                }
            }
        }
    </script>
</body>

</html>